package org.examples.java.lang;

import org.junit.Assert;
import org.junit.Test;

public class BooleanTest
{

    @Test
    public void testConstructor() throws Exception
    {
        Boolean bool = new Boolean(true);
        Assert.assertTrue(bool);

        Boolean bool2 = new Boolean(false);
        Assert.assertFalse(bool2);

        Boolean bool3 = new Boolean("tRuE");
        Assert.assertTrue(bool3);

        Assert.assertNotSame(bool, bool3);
    }

    @Test
    public void valueOf() throws Exception
    {
        Assert.assertSame(Boolean.TRUE, Boolean.valueOf("TRuE"));
        Assert.assertSame(Boolean.FALSE, Boolean.valueOf("False"));
        Assert.assertSame(Boolean.FALSE, Boolean.valueOf(null));
        Assert.assertSame(Boolean.FALSE, Boolean.valueOf("XXX"));

        Assert.assertSame(Boolean.TRUE, Boolean.valueOf(true));
        Assert.assertSame(Boolean.FALSE, Boolean.valueOf(false));

    }

    @Test
    public void _toString() throws Exception
    {
        Assert.assertEquals("true", Boolean.TRUE.toString());
        Assert.assertEquals("false", Boolean.FALSE.toString());
        Assert.assertEquals("true", Boolean.toString(true));
        Assert.assertEquals("false", Boolean.toString(false));
    }

    @Test
    public void parseBoolean() throws Exception
    {
        Assert.assertTrue(Boolean.parseBoolean("true"));
        Assert.assertTrue(Boolean.parseBoolean("truE"));
        Assert.assertFalse(Boolean.parseBoolean("False"));
        Assert.assertFalse(Boolean.parseBoolean("XXX"));
        Assert.assertFalse(Boolean.parseBoolean(null));
    }

    @Test
    public void getBoolean() throws Exception
    {
        Assert.assertFalse(Boolean.getBoolean("non-existent-boolean-param"));
        Assert.assertFalse(Boolean.getBoolean(null));
    }

    @Test
    public void testConstants() throws Exception
    {
        // JVM should auto-box primitive values to Boolean.TRUE or Boolean.FALSE
        Assert.assertSame(Boolean.TRUE, true);
        Assert.assertSame(Boolean.FALSE, false);
    }

    @Test
    public void booleanValue() throws Exception
    {
        Assert.assertTrue(Boolean.valueOf(true).booleanValue());
        Assert.assertFalse(Boolean.valueOf(false).booleanValue());
    }

    @Test
    public void testHashCode() throws Exception
    {
        Assert.assertEquals(1231, Boolean.valueOf(true).hashCode());
        Assert.assertEquals(1237, Boolean.valueOf(false).hashCode());
    }

    @Test
    public void testEquals() throws Exception
    {
        Assert.assertTrue(Boolean.valueOf(true).equals(true));
        Assert.assertTrue(Boolean.valueOf(true).equals(Boolean.TRUE));
        Assert.assertTrue(Boolean.valueOf(false).equals(false));
        Assert.assertTrue(Boolean.valueOf(false).equals(Boolean.FALSE));
        Assert.assertFalse(Boolean.valueOf(false).equals(true));
        Assert.assertFalse(Boolean.valueOf(true).equals("true"));
        Assert.assertFalse(Boolean.valueOf(false).equals("false"));
    }

    @Test
    public void testCompareTo() throws Exception
    {
        Assert.assertTrue(Boolean.valueOf(true).compareTo(true) == 0);
        Assert.assertTrue(Boolean.valueOf(true).compareTo(false) > 0);
        Assert.assertTrue(Boolean.valueOf(false).compareTo(true) < 0);
    }
}
